Skip to content

Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | improve_with_precomputing#128

Open
RahwaZeslusHaile wants to merge 3 commits intoCodeYourFuture:mainfrom
RahwaZeslusHaile:improve_with_precomputing
Open

Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | improve_with_precomputing#128
RahwaZeslusHaile wants to merge 3 commits intoCodeYourFuture:mainfrom
RahwaZeslusHaile:improve_with_precomputing

Conversation

@RahwaZeslusHaile
Copy link

@RahwaZeslusHaile RahwaZeslusHaile commented Feb 27, 2026

Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | improve_with_precomputing

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Description

Improves the performance of two algorithms using precomputing strategies without modifying existing tests.

Changelist

Common Prefix:
Pre-sort strings once, then compare only adjacent pairs
Reduces comparisons from O(n²) to O(n log n) sort + O(n) comparisons

Count Letters:
Precompute lowercase letters into a set during the first pass
Replace repeated letter.lower() in s checks with O(1) set lookups
Reduces time complexity from O(n²) to O(n)

I really appreciate the time you take to review this PR

@RahwaZeslusHaile RahwaZeslusHaile added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 27, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 27, 2026
@github-actions

This comment has been minimized.

@RahwaZeslusHaile RahwaZeslusHaile added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 28, 2026
Copy link

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good.

Can you use complexity to explain how the new implementation are better than the original implementation?

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Mar 2, 2026
@RahwaZeslusHaile
Copy link
Author

Thanks @cjyuan for that feedback! I've documented the complexity analysis in the README.

@RahwaZeslusHaile RahwaZeslusHaile added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 2, 2026
- Time Complexity: **O(n² × m)** - Compares every string with every other string (n²) pairs, each taking O(m) comparisons

**Optimized Approach (Sort + Adjacent Pairs):**
- Time Complexity: **O(n log n + n × m)** - Sort once (n log n), then compare only adjacent pairs (n comparisons)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are factoring in the length of the strings, m, then the complexity of sorting won't just be O(nlogn). nlogn measures only the number of comparisons needed.


**Optimized Approach (Sort + Adjacent Pairs):**
- Time Complexity: **O(n log n + n × m)** - Sort once (n log n), then compare only adjacent pairs (n comparisons)
- **Why better:** The longest common prefix will be found in the first and last strings after sorting, eliminating unnecessary comparisons. Reduces from O(n²) to O(n) pair comparisons.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by first and last strings? That sounds like only two comparisons are needed to find the longest prefix after sorting.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @cjyuan for the detailed feedback! You're absolutely right on both points. I've updated the README to correct these issues

@cjyuan cjyuan removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 2, 2026
@RahwaZeslusHaile RahwaZeslusHaile added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 2, 2026
@cjyuan
Copy link

cjyuan commented Mar 2, 2026

Good analysis. Well done!

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Reviewed Volunteer to add when completing a review with trainee action still to take. labels Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants